import { _decorator, Component, Node, Vec2, EventTouch, UITransform, Vec3, misc, PlaneCollider } from 'cc';
import { KeyInputManager } from './key-input-controller';
import { PlayerController } from './player-controller';
const { ccclass, property } = _decorator;

@ccclass('GameController')
export class GameController extends Component {
    @property({ type: Node })
    nodeMovePanel: Node = null!;
    @property({ type: Node })
    nodeRotatePanel: Node = null!;


    @property({ type: Node })
    nodeVirtualRocker: Node = null!;
    @property({ type: Node })
    nodeVirtualRockerPoint: Node = null!;

    @property({ type: Node })
    nodePlayer: Node = null!;

    private _playerController: PlayerController = null;


    private _uiTrans: UITransform = null!;
    private _uiTransVirtualRocker: UITransform = null!;

    private _keyInputManager: KeyInputManager = null!;

    private _tempRotateVec2: Vec2 = null!;

    onLoad() {
        this._keyInputManager = new KeyInputManager();
    }

    start() {
        this._uiTrans = this.nodeMovePanel.getComponent(UITransform);
        this._uiTransVirtualRocker = this.nodeVirtualRocker.getComponent(UITransform);
        this._playerController = this.nodePlayer.getComponent(PlayerController);

        this.initEvent();
    }
    private initEvent(): void {
        this.nodeMovePanel.on(Node.EventType.TOUCH_START, this.nodeMoveTouchStart, this);
        this.nodeMovePanel.on(Node.EventType.TOUCH_MOVE, this.nodeMoveTouchMove, this);
        this.nodeMovePanel.on(Node.EventType.TOUCH_END, this.nodeMoveTouchEnd, this);
        this.nodeMovePanel.on(Node.EventType.TOUCH_CANCEL, this.nodeMoveTouchEnd, this);

        this.nodeRotatePanel.on(Node.EventType.TOUCH_START, this.nodeRotateTouchStart, this);
        this.nodeRotatePanel.on(Node.EventType.TOUCH_MOVE, this.nodeRotateTouchMove, this);
        this.nodeRotatePanel.on(Node.EventType.TOUCH_END, this.nodeRotateTouchEnd, this);
        this.nodeRotatePanel.on(Node.EventType.TOUCH_CANCEL, this.nodeRotateTouchEnd, this);
    }
    private nodeMoveTouchStart(event: EventTouch): void {
        let pos: Vec2 = event.getUILocation();
        let nodePos: Vec3 = this._uiTrans.convertToNodeSpaceAR(new Vec3(pos.x, pos.y, 0));
        this.nodeVirtualRocker.position = nodePos;
    }
    private nodeMoveTouchMove(event: EventTouch): void {
        let pos: Vec2 = event.getUILocation();
        let nodePos: Vec3 = this._uiTransVirtualRocker.convertToNodeSpaceAR(new Vec3(pos.x, pos.y, 0));

        let isRun: boolean = false;

        let dis: number = Vec3.distance(nodePos, new Vec3(0, 0, 0));
        let up: Vec3 = new Vec3(0, 1, 0);
        let radian: number = Vec3.angle(up, nodePos);
        let angle: number = misc.radiansToDegrees(radian);

        let across: number = new Vec2(0, 1).cross(new Vec2(nodePos.x, nodePos.y));
        if (dis > 98) {
            let x: number = 98 * Math.sin(radian);
            let y: number = 98 * Math.cos(radian);

            if (across > 0) {
                x *= -1;
            }
            this.nodeVirtualRockerPoint.position = new Vec3(x, y, 0);
            if (nodePos.y > 190) {
                isRun = true;
            }
        } else {
            this.nodeVirtualRockerPoint.position = nodePos;

        }
        if (across < 0) {
            angle *= -1;
        }
        this._playerController.move(angle, isRun);
    }
    private nodeMoveTouchEnd(event: EventTouch): void {
        this.nodeVirtualRocker.position = new Vec3(-440, -100, 0);
        this.nodeVirtualRockerPoint.position = new Vec3(0, 0, 0);
        this._playerController.stopMove();
    }

    private nodeRotateTouchStart(event: EventTouch): void {
        this._tempRotateVec2 = event.getUILocation();
    }
    private nodeRotateTouchMove(event: EventTouch): void {
        let pos: Vec2 = event.getUILocation();
        let x: number = pos.x - this._tempRotateVec2.x;
        let y: number = pos.y - this._tempRotateVec2.y;
        this._playerController.rotate(x, y);
        this._tempRotateVec2 = pos;

    }
    private nodeRotateTouchEnd(event: EventTouch): void {

    }



    update(deltaTime: number) {

    }
}


